<?php
namespace mvc\validator;

class StringValidator extends Validator
{
    public $min;
    public $max;

    public function validate()
    {
        if (!is_string($this->value)) {
            $this->message = $this->attributeLabel . '必须为字符串';
            return false;
        }

        if($this->min && !$this->max) {
            if(mb_strlen($this->value, 'utf-8') < $this->min) {
                $this->message = $this->attributeLabel . '至少' . $this->min . '位';
                return false;
            }
        }

        if(!$this->min && $this->max) {
            if(mb_strlen($this->value, 'utf-8') > $this->max) {
                $this->message = $this->attributeLabel . '至多' . $this->max . '位';
                return false;
            }
        }

        if($this->min && $this->max) {
            if(!(mb_strlen($this->value, 'utf-8') >= $this->min && mb_strlen($this->value, 'utf-8') <= $this->max)) {
                $this->message = $this->attributeLabel . '至少' . $this->min . '位,' . '至多' . $this->max . '位';
                return false;
            }
        }

        return true;
    }

    public function getMessage()
    {
        return $this->message;
    }
}